import { Component, OnInit } from '@angular/core';
import { AgentService } from '../../agent/common-services/agent.service';
import { PropertyService } from '../service/property.service';
import { ActivatedRoute, Router } from '@angular/router';
import { PropertyType, CRUD_MODE } from '../../shared/config/app-settings';
import * as _ from 'lodash';
import { TranslateService } from '@ngx-translate/core';
import { UserService } from '../../shared/services/user.service';
@Component({
  selector: 'app-customer-property-list',
  templateUrl: './customer-property-list.component.html',
  styleUrls: ['./customer-property-list.component.css']
})
export class CustomerPropertyListComponent implements OnInit {
  userId: number;
  cols: Array<any> = [];
  selectedColumns: Array<any> = [];
  records: Array<any> = [];
  constructor(private agentService: AgentService,
    private PropertyService: PropertyService,
    private userService: UserService,
    private translateService: TranslateService,
    private router: Router,
    private route: ActivatedRoute) { }

  ngOnInit() {
    this.cols = [
      { field: 'id', header: 'ID' },
      { field: 'purchaseDate', header: 'DATE' },
      { field: 'BuildingAddress', header: 'BUILDING ADDRESS' },
      { field: 'ClientSurname', header: 'CLIENT SURNAME' },
      { field: 'Agent', header: 'AGENT' },
      { field: 'lot', header: 'LOT#' },
      { field: 'sale_status', header: 'STATUS' }
    ];

    this.selectedColumns = _.cloneDeep(this.cols); //toggle cols
    this.selectedColumns.splice(0, 1); //default hide first column

    this.route.params.subscribe(params => {
      this.userId = +params['user-id']; // (+) converts string 'id' to a number
      console.log('login userid : ' + this.userId);
      // In a real app: dispatch action to load the details here.
      this.load();
    });


  }

  //ui event
  reload() {
    this.load();
  }

  load() {
    let filter = {
      ownerId: this.userId,
      type: PropertyType.SALE
    };
    this.PropertyService.getAllProperties(filter).subscribe(
      data => {
        console.log(data);
        this.records = data;
        _.each(this.records, (item) => {

          item.BuildingAddress = item.propertyList ? item.propertyList.buildingAddress : '';
          item.ClientSurname = item.owner ? item.owner.lastName : '';
          item.Agent = item.agent ? item.agent.lastName : '';
          console.log(item);

        });
      },
      err => {
        console.log(err);
      }
    )
  }

  onViewRow(rowData) {
    this.router.navigate(['../../property-detail/' + rowData.id], { relativeTo: this.route, queryParams: { menuKey: 'house-detail', mode: 'view' } });
  }


}
